package com.example.textswitchtestdemo01.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.bean.Material;
import com.example.textswitchtestdemo01.util.InitToolbar;
import com.google.gson.Gson;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class SeventhShopActivity extends AppCompatActivity {
    private TextView tv_total_price;
    private LinearLayout ll_price_seven_shop;
    private LinearLayout ll_count_seven_shop;
    private ListView lv_seventh_shop;
    private ArrayAdapter<Material.DataBean> arrayAdapter;
    private List<Material.DataBean> list = new ArrayList<>();
    private final DecimalFormat DF = new DecimalFormat("#,###");
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seventh_shop);
        initView();
        InitToolbar.initToolbar(this, "已购商品", true);


        getData();

    }

    private void getData() {
        Intent intent = getIntent();
        String json = intent.getStringExtra("json");
        Toast.makeText(this, json, Toast.LENGTH_SHORT).show();
        Material material = new Gson().fromJson(json, Material.class);
        if (material != null) {
            List<Material.DataBean> data = material.getData();
            if (data != null && !data.isEmpty()) {
                list.clear();
                list.addAll(data);
                int total = 0;
                for (Material.DataBean bean : list) {
                    total += (bean.getNum() * bean.getPrice());
                }
                tv_total_price.setText(DF.format(total));
                arrayAdapter.notifyDataSetChanged();
            } else {
                Toast.makeText(this, "data null", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "shopping null", Toast.LENGTH_SHORT).show();
        }


    }

    private void initView() {
        tv_total_price = (TextView) findViewById(R.id.tv_total_price);
        ll_price_seven_shop = (LinearLayout) findViewById(R.id.ll_price_seven_shop);
        ll_count_seven_shop = (LinearLayout) findViewById(R.id.ll_count_seven_shop);
        lv_seventh_shop = (ListView) findViewById(R.id.lv_seventh_shop);

        arrayAdapter = new ArrayAdapter<Material.DataBean>(getApplicationContext(), R.layout.item_lv_seven, list) {
            class ViewHolder {
                TextView tv_count_lv_seven;
                TextView tv_price_lv_seven;
                TextView tv_gongyingshang_lv_seven;
                TextView tv_name_lv_seven;
                TextView tv_id_lv_seven;
            }

            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_lv_seven, parent, false);
                    holder = new ViewHolder();
                    holder.tv_count_lv_seven = convertView.findViewById(R.id.tv_count_lv_seven);
                    holder.tv_gongyingshang_lv_seven = convertView.findViewById(R.id.tv_gongyingshang_lv_seven);
                    holder.tv_id_lv_seven = convertView.findViewById(R.id.tv_id_lv_seven);
                    holder.tv_name_lv_seven = convertView.findViewById(R.id.tv_name_lv_seven);
                    holder.tv_price_lv_seven = convertView.findViewById(R.id.tv_price_lv_seven);

                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                Material.DataBean item = getItem(position);
                holder.tv_price_lv_seven.setText(DF.format(item.getPrice()));
                holder.tv_name_lv_seven.setText(item.getMaterialName());
                holder.tv_gongyingshang_lv_seven.setText(item.getContent());
                holder.tv_id_lv_seven.setText(item.getId() + "");
                holder.tv_count_lv_seven.setText(item.getNum() + "");
                return convertView;
            }
        };

        lv_seventh_shop.setAdapter(arrayAdapter);
    }


}
